var express = require('express');
var router = express.Router();
const { Conversation } = require('../module/Conversation');

// 添加新的聊天室

router.post('/', async (req, res) => {
    const newConversation = new Conversation({
        members: [req.body.senderId, req.body.receiverId]
    })
    try {
        const savedConversation = await newConversation.save();
        res.status(200).json(savedConversation);
    }
    catch (err) {
        res.status(500).json(err)
    }

})

//获取到当前用户拥有的聊天室

router.get("/:userId", async (req, res) => {
    try {
        const conversation = await Conversation.find({
            members: { $in: [req.params.userId] }
        })
        res.status(200).json(conversation)
    }
    catch (err) {
        res.status(500).json(err)
    }
}
)

module.exports = router;